package com.ruoyi.web.controller.system;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.domain.entity.SysProject;
import com.ruoyi.common.core.domain.entity.SysProjectImage;
import com.ruoyi.common.core.page.ResultEntity;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.MinioTemplate;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.service.ISysAdminProjectService;
import com.ruoyi.system.service.ISysProjectImageService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * @Description TODO
 * @Version 1.0.0
 * @Date 2024/10/11 10:41
 * @Author wux
 */
@Api(tags = "项目管理")
@RestController
@RequestMapping("/client/project")
public class SysProjectController {
    @Autowired
    private ISysAdminProjectService sysAdminProjectService;
    @Autowired
    private ISysProjectImageService sysProjectImageService;
    @Autowired
    private MinioTemplate minioTemplate;

    @GetMapping("/getProjectInfo")
    @Log(title = "查询项目介绍", businessType = BusinessType.INSERT)
    @ApiOperation(value = "查询项目介绍",response = SysProject.class)
    public ResultEntity getProjectInfo(){
        //项目暂时写死1
        SysProject project = sysAdminProjectService.getOne(new LambdaQueryWrapper<SysProject>()
                .eq(SysProject::getId,1L));
        //查询项目图片
        List<SysProjectImage> imageList = sysProjectImageService.list(new LambdaQueryWrapper<SysProjectImage>()
                .eq(SysProjectImage::getProjectId,1L)
                .eq(SysProjectImage::getDeleted,0));
        project.setImageList(imageList);
        return ResultEntity.createSuccess(project);
    }

    @PostMapping("/updateContent")
    @Log(title = "修改项目介绍", businessType = BusinessType.INSERT)
    @ApiOperation(value = "修改项目状态")
    public ResultEntity update(@RequestBody String content){
        return ResultEntity.createSuccess(sysAdminProjectService.update(new LambdaUpdateWrapper<SysProject>()
                .set(SysProject::getContent,content)
                .eq(SysProject::getId,1L)));
    }

    @PostMapping("/saveImage")
    @Log(title = "项目图片保存", businessType = BusinessType.INSERT)
    @ApiOperation(value = "项目图片保存")
    public ResultEntity saveImage(@RequestBody SysProjectImage params){
        params.setProjectId(1L);
        return ResultEntity.createSuccess(sysProjectImageService.save(params));
    }

    @PostMapping("/delImage")
    @Log(title = "项目图片删除", businessType = BusinessType.INSERT)
    @ApiOperation(value = "项目图片删除")
    public ResultEntity delImage(@RequestBody SysProjectImage params){
        params.setDeleted(1);
        return ResultEntity.createSuccess(sysProjectImageService.updateById(params));
    }

    @PostMapping("/upload")
    @ApiOperation(value = "项目图片上传并保存")
    public ResultEntity upload(@RequestParam("file") MultipartFile file) throws Exception {
        String filePath = minioTemplate.upload(file);
        SysProjectImage projectImage = new SysProjectImage();
        projectImage.setProjectId(1L);
        projectImage.setPath(filePath);
        projectImage.setCreateBy(SecurityUtils.getLoginUser().getUser().getId());
        projectImage.setUpdateBy(SecurityUtils.getLoginUser().getUser().getId());
        sysProjectImageService.save(projectImage);
        return ResultEntity.createSuccess(filePath);
    }

}
